/**
 * Created by lx on 2017/10/16.
 */

/*mobiscroll.settings = {
    theme: 'ios',
    display: 'bottom'
};*/
//边框闪烁
function blinklink(count, dom) {
    //转换jq对象
    var judgedom = dom instanceof jQuery;
    if (!judgedom) {
        dom = $(dom)
    }
    if (count <= 0) {
        //清除定时器
        dom.css("border-bottom-color", "#bf1e2b");
        clearTimeout(timer);
        return;
    }else{
        //从rgb转成hex;
        var domcolor = dom.css("border-bottom-color");
        var domtips = domcolor.substring(domcolor.indexOf("(") + 1, domcolor.indexOf(")"));
        var splitafter = domtips.split(",");
        var lastreture = rgbToHex(Number(splitafter[0]), Number(splitafter[1]), Number(splitafter[2]));
        if (!lastreture) {
            dom.css("border-bottom-color", "#bf1e2b")
        }
        if (lastreture == "#bf1e2b") {
            dom.css("border-bottom-color", "#a7a7a7")
        } else {
            dom.css("border-bottom-color", "#bf1e2b")
        }
        count--;
    }
    timer = setTimeout(function(){
        blinklink(count,dom)
    }, 200);


}
function componentToHex(c) {
    var hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;

}
function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
function getstart(time,flag){
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    if(flag=="0"){
        var dateMin=year + '-' + month + '-' + date + ' 00:00:00';
    }else if(flag=="1"){
        var dateMin=year + '-' + month + '-' + date + ' 23:59:59';
    }else{
        var dateMin=year + '-' + month + '-' + date + ' 12:00:00';
    }
    return dateMin;
}
function echartime(time,tags){
    var year = (time.toString()).substring(0,4);
    var month =(time.toString()).substring(4,6);
    var date = (time.toString()).substring(6,8);
    if(tags=='0'){
        var dateMin=year + '-' + month + '-' + date + ' 12:00:00';
    }else{
        var dateMin= + month + '/' + date;
    }
    return dateMin;
}
//时间改时间戳
function jonsr(str){
    var date = new Date(str.replace(/-/g, '/'));
    return date.getTime();
}
//压缩图片
var ImageResizer=function(opts){
    var settings={
        fixedSize:false,
        resizeMode:"auto"//压缩模式，总共有三种  auto,width,height auto表示自动根据最大的宽度及高度等比压缩，width表示只根据宽度来判断是否需要等比例压缩，height类似。
        ,dataSource:"" //数据源。数据源是指需要压缩的数据源，有三种类型，image图片元素，base64字符串，canvas对象，还有选择文件时候的file对象。。。
        ,dataSourceType:"image" //image  base64 canvas
        ,maxWidth:500 //允许的最大宽度
        ,maxHeight:500 //允许的最大高度。
        ,onTmpImgGenerate:function(img){} //当中间图片生成时候的执行方法。。这个时候请不要乱修改这图片，否则会打乱压缩后的结果。
        ,success:function(resizeImgBase64,canvas){

        }//压缩成功后图片的base64字符串数据。
    };
    var appData={};
    $.extend(settings,opts);

    var innerTools={
        getBase4FromImgFile:function(file,callBack){

            var reader = new FileReader();
            reader.onload = function(e) {
                var base64Img= e.target.result;
                if(callBack){
                    callBack(base64Img);
                }
            };
            reader.readAsDataURL(file);
        },

        //--处理数据源。。。。将所有数据源都处理成为图片对象，方便处理。
        getImgFromDataSource:function(datasource,dataSourceType,callback){
            var _me=this;
            var img1=new Image();
            if(dataSourceType=="img"||dataSourceType=="image"){
                img1.src=$(datasource).attr("src");
                if(callback){
                    callback(img1);
                }
            }
            else if(dataSourceType=="base64"){
                img1.src=datasource;
                if(callback){
                    callback(img1);
                }
            }
            else if(dataSourceType=="canvas"){
                img1.src = datasource.toDataURL("image/jpeg");
                if(callback){
                    callback(img1);
                }
            }
            else if(dataSourceType=="file"){
                _me.getBase4FromImgFile(function(base64str){
                    img1.src=base64str;
                    if(callback){
                        callback(img1);
                    }
                });
            }
        },

        //计算图片的需要压缩的尺寸。当然，压缩模式，压缩限制直接从setting里面取出来。
        getResizeSizeFromImg:function(img){
            var _img_info={
                w:$(img)[0].naturalWidth,
                h:$(img)[0].naturalHeight
            };

            var _resize_info={
                w:0,
                h:0
            };

            if(_img_info.w <= settings.maxWidth && _img_info.h <= settings.maxHeight){
                return _img_info;
            }
            if(settings.resizeMode=="auto"){
                var _percent_scale=parseInt(_img_info.w/_img_info.h);
                var _size1={
                    w:0,
                    h:0
                };
                var _size_by_mw={
                    w:settings.maxWidth,
                    h:parseInt(settings.maxWidth/_percent_scale)
                };
                var _size_by_mh={
                    w:parseInt(settings.maxHeight*_percent_scale),
                    h:settings.maxHeight
                };
                if(_size_by_mw.h <= settings.maxHeight){
                    return _size_by_mw;
                }
                if(_size_by_mh.w <= settings.maxWidth){
                    return _size_by_mh;
                }

                return {
                    w:settings.maxWidth,
                    h:settings.maxHeight
                };
            }
            if(settings.resizeMode=="width"){
                if(_img_info.w<=settings.maxWidth){
                    return _img_info;
                }
                var _size_by_mw={
                    w:settings.maxWidth
                    ,h:parseInt(settings.maxWidth/_percent_scale)
                };
                return _size_by_mw;
            }
            if(settings.resizeMode=="height"){
                if(_img_info.h<=settings.maxHeight){
                    return _img_info;
                }
                var _size_by_mh={
                    w:parseInt(settings.maxHeight*_percent_scale)
                    ,h:settings.maxHeight
                };
                return _size_by_mh;
            }
        },

        //--将相关图片对象画到canvas里面去。
        drawToCanvas:function(img,theW,theH,realW,realH,callback){
            var canvas = document.createElement("canvas");
            canvas.width=theW;
            canvas.height=theH;
            var ctx = canvas.getContext('2d');
            ctx.drawImage(img,
                0,//sourceX,
                0,//sourceY,
                realW,//sourceWidth,
                realH,//sourceHeight,
                0,//destX,
                0,//destY,
                theW,//destWidth,
                theH//destHeight
            );
            if(settings.fixedSize){
                //重新设置画布的大小
                canvas.setAttribute("width",800);
                canvas.setAttribute("height",480);
                //将获得的图像副本,重新绘制到画布,完成画布的大小改变
                ctx.drawImage(img,0,0,800,480);
                //--获取base64字符串及canvas对象传给success函数。
            }
            var base64str=canvas.toDataURL("image/jpeg");
            if(callback){
                callback(base64str,canvas);
            }
        }
    };

    //--开始处理。
    (function(){
        innerTools.getImgFromDataSource(settings.dataSource,settings.dataSourceType,function(_tmp_img){
            _tmp_img.onload = function(){
                var __tmpImg=_tmp_img;
                settings.onTmpImgGenerate(_tmp_img);

                //--计算尺寸。
                var _limitSizeInfo=innerTools.getResizeSizeFromImg(__tmpImg);
                var _img_info={
                    w:$(__tmpImg)[0].naturalWidth,
                    h:$(__tmpImg)[0].naturalHeight
                };
                innerTools.drawToCanvas(__tmpImg,_limitSizeInfo.w,_limitSizeInfo.h,_img_info.w,_img_info.h,function(base64str,canvas){
                    settings.success(base64str,canvas);
                });
            }
        });
    })();

    var returnObject={


    };

    return returnObject;
};
function dataURLtoBlob(dataurl) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], {type:mime});
}

//amr文件转码播放暂不支持ie

var gAudioContext = new AudioContext();
//var audioSrc = null;
function getAudioContext() {
    if (!gAudioContext) {
        gAudioContext = new AudioContext();
    }
    return gAudioContext;
}

function fetchBlob(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.responseType = 'blob';
    xhr.onload = function() {
        callback(this.response);
    };
    xhr.onerror = function() {
        alert("获取音频失败");
    };
    xhr.send();
}
function readBlob(blob, callback) {
    var reader = new FileReader();
    reader.onload = function(e) {
        var data = new Uint8Array(e.target.result);
        callback(data);
    };
    reader.readAsArrayBuffer(blob);
}
function playAmrBlob(blob,callback) {
    readBlob(blob, function(data) {
        playAmrArray(data,callback);
    });
}
function playAmrArray(array,callback) {
    var samples = AMR.decode(array);
    if (!samples) {
        alert('Failed to decode!');
        return;
    }
    setPcm(samples,callback);
}
function playPcm(samples) {
    var ctx = getAudioContext();
    var audioSrc = ctx.createBufferSource();
    var buffer = ctx.createBuffer(1, samples.length, 8000);
    if (buffer.copyToChannel) {
        buffer.copyToChannel(samples, 0, 0)
    } else {
        var channelBuffer = buffer.getChannelData(0);
        channelBuffer.set(samples);
    }
    audioSrc.buffer = buffer;
    audioSrc.connect(ctx.destination);
    audioSrc.start();
}
function setPcm(samples,callback){
    var ctx = getAudioContext();
    var audioSrc = ctx.createBufferSource();
    var buffer = ctx.createBuffer(1, samples.length, 8000);
    if (buffer.copyToChannel) {
        buffer.copyToChannel(samples, 0, 0)
    } else {
        var channelBuffer = buffer.getChannelData(0);
        channelBuffer.set(samples);
    }
    audioSrc.buffer = buffer;
    audioSrc.connect(ctx.destination);
    callback(audioSrc);
}


function timeFilter(time_str) {
    var time_obj = {
        hour: 0,
        minute: 0,
        text: "",
    };
    var str_len = time_str.length;
    time_obj.hour=time_str.substring(0, str_len - 2)
    time_obj.minute = time_str.substring(str_len - 2, str_len);
    time_obj.text = time_obj.hour + ':' + time_obj.minute;

    return time_obj;
}

function moveSpace(str) {
    var reg = /[\u4E00-\u9FA5]/g;
    var result = str.replace(reg, '');
    var afterstr = result.replace(/[ ]/g, "");
    return afterstr;
}

function randomWord(randomFlag, min, max) {
    var str = "",
        range = min,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    // 随机产生
    if (randomFlag) {
        range = Math.round(Math.random() * (max - min)) + min;
    }
    for (var i = 0; i < range; i++) {
        pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}

//正则表达式判断网址是否合法
function IsURL(url){
    var adurl;
    // var re = /^(?:http(?:s|):\/\/|)(?:(?:\w*?)\.|)(?:\w*?)\.(?:\w{2,4})(?:\?.*|\/.*|)$/ig
    var strRegex="^((https|http|ftp|rtsp|mms)?://)"
        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
        + "|" // 允许IP和DOMAIN（域名）
        + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
        + "[a-z]{2,6})" // first level domain- .com or .museum
        + "(:[0-9]{1,4})?" // 端口- :80
        + "((/?)|" // a slash isn't required if there is no file name
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re=new RegExp(strRegex);
    if (re.test(url)){
        if (url.indexOf("http://") == -1){
            adurl = "http://"+url;
        }else{
            adurl = url;
        }
        return adurl;
    }
    return adurl;
}
//判断手机微信操作系统
function ismobile(){
    var u = navigator.userAgent, app = navigator.appVersion;
    if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
        if(window.location.href.indexOf("?mobile")<0){
            try{
                if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
                    return '0';
                }else{
                    return '1';
                }
            }catch(e){}
        }
    }else if( u.indexOf('iPad') > -1){
        return '0';
    }else{
        return '1';
    }
};

// 二维码图片拖拽
function ImgDrg(opts){
    if(!opts.boxId){
        alert("请设置拖拽模块");
        return;
    }
    if(!opts.parentboxId){
        alert("请设置拖拽区域");
        return;
    }
    //初始化参数设置
    this.defaults = {
        boxId:"",
        coorId:"",
        parentboxId:"",
        boxMove:"",
        coorMove:"",
        upFuc:"",
        advert:""
    };
    this._right = 0;//初始化二维码框右边和底边到边框的距离
    this._bottom = 0;
    $.extend(this.defaults,opts);//继承外部自定义类
    var EventUtil = eventUtil();
    var _self = this;//将当前对象先赋值储存
    this.box = document.getElementById(this.defaults.boxId);
    // this.coor = document.getElementById(this.defaults.coorId);
    this.coor = document.querySelectorAll("."+this.defaults.coorId);
    this.big = $('#'+this.defaults.parentboxId);
    this.$box = $(this.box);
    this.bigStyle = {
        width:_self.big.width(),
        height:_self.big.height()
    };
    EventUtil.add(this.box, eventType("startEvent"), mousedown);
    // EventUtil.add(_self.coor, 'mousedown', mousedown);
    for(var i=0;i<this.coor.length;i++){
        EventUtil.add(this.coor[i], eventType("startEvent"), mousedown);
    }

    function mousedown(e){
        event = EventUtil.getEvent(event); // 跨浏览器获取 event 对象
        EventUtil.preventDefault(event);   // 阻止默认事件
        EventUtil.stopPropagation(event);  // 阻止默认事件
        _self.move = this;
        _self.posix = {
            'startPageX': initXY(e, "x"),
            'startPageY': initXY(e, "y"),
            'x':parseFloat(_self.$box.css("left")),
            'y':parseFloat(_self.$box.css("top")),
            'w': _self.$box.width(),
            'h': _self.$box.height()
        };
        _self._right = _self.posix.x + _self.posix.w;
        _self._bottom = _self.posix.y + _self.posix.h;
        EventUtil.add(document, eventType("moveEvent"), mousemove);
        EventUtil.add(document, eventType("endEvent"), mouseup);
    }
    function mousemove(e){
        event = EventUtil.getEvent(event); // 跨浏览器获取 event 对象
        EventUtil.preventDefault(event); // 阻止默认事件
        EventUtil.stopPropagation(event);  // 阻止默认事件
        //if(initXY(e, "x")%1 !=0 || initXY(e, "y")%1 !=0){
        //    return;
        //}
        var pageX = initXY(e, "x") - _self.posix.startPageX;
        var pageY = initXY(e, "y") - _self.posix.startPageY;
        if(_self.move.id == _self.defaults.boxId){
            var top = pageY + _self.posix.y;
            var left = pageX + _self.posix.x;
            if(top<0){
                top = 0;
            }else if(top>(_self.bigStyle.height-_self.$box.height())){
                top = _self.bigStyle.height-_self.$box.height();
            }
            if(left<0){
                left = 0;
            }else if(left > _self.bigStyle.width - _self.$box.width()){
                left = _self.bigStyle.width - _self.$box.width();
            }
            $(_self.box).css({
                'top': top,
                'left': left,
            });
            var boxLeft = parseFloat(_self.$box.css("left")),boxTop = parseFloat(_self.$box.css("top"));
            if(typeof _self.defaults.boxMove == "function"){
                if(typeof _self.defaults.advert == "object"){
                    _self.defaults.boxMove(boxLeft,boxTop,_self.defaults.advert);
                }else{
                    _self.defaults.boxMove(boxLeft,boxTop);
                }
            }
            _self.posix = {'startPageX': initXY(e, "x") , 'startPageY': initXY(e, "y"),'x':boxLeft,'y':boxTop};
        }else if(_self.move.className.indexOf(_self.defaults.coorId)>-1){
            var isMove = true;
            var max_width,max_height,left_top = {
                'left':parseFloat(_self.$box.css("left")),
                'top':parseFloat(_self.$box.css("top"))
            },maxStyle,changePage,MaxStyle;
            switch (_self.move.id){
                case "coor1":
                    changePage = -getChangePage(pageX,pageY,"x");
                    max_width = changePage + _self.posix.w;
                    max_height = changePage + _self.posix.h;
                    left_top.left -= (changePage);
                    left_top.top -= (changePage);
                    if(left_top.top<0){
                        isMove = false;
                        left_top.top = 0;
                    }
                    if(left_top.left<0){
                        isMove = false;
                        left_top.left = 0;
                    }
                    MaxStyle = setMaxStyle(max_height,left_top,max_width);
                    maxStyle = MaxStyle.maxStyle;
                    if(isMove){
                        isMove = MaxStyle.isMove;
                    }
                    var _left = _self._right -maxStyle;
                    var _top = _self._bottom -maxStyle;
                    if(_left<0){
                        _top -= _left;
                        _left = 0;
                    }
                    if(_top<0){
                        _left -= _top;
                        _top = 0;
                    }
                    if(_self._right -maxStyle>0){

                    }
                    left_top.left = _left;
                    left_top.top = _top;
                    break;
                case "coor2":
                    changePage = -getChangePage(pageX,pageY,"y");
                    max_width = changePage + _self.posix.w;
                    max_height = changePage + _self.posix.h;
                    left_top.top -= changePage;
                    if(left_top.top<0){
                        left_top.top = 0;
                        isMove = false;
                    }
                    MaxStyle = setMaxStyle(max_height,left_top,max_width);
                    maxStyle = MaxStyle.maxStyle;
                    if(isMove){
                        isMove = MaxStyle.isMove;
                    }
                    //maxStyle = max_height;
                    break;
                case "coor3":
                    changePage = getChangePage(pageX,pageY,"x");
                    max_width = changePage + _self.posix.w;
                    max_height = changePage + _self.posix.h;
                    MaxStyle = setMaxStyle(max_height,left_top,max_width);
                    maxStyle = MaxStyle.maxStyle;
                    if(isMove){
                        isMove = MaxStyle.isMove;
                    }
                    break;
                case "coor4":
                    changePage = getChangePage(pageX,pageY,"y");
                    max_width = changePage + _self.posix.w;
                    max_height = changePage + _self.posix.h;
                    left_top.left -= (changePage);
                    if(left_top.left<0){
                        left_top.left = 0;
                        isMove = false;
                    }
                    MaxStyle = setMaxStyle(max_height,left_top,max_width);
                    maxStyle = MaxStyle.maxStyle;
                    if(isMove){
                        isMove = MaxStyle.isMove;
                    }
                    left_top.left = _self._right -maxStyle;
                    break;
            }
            var setStyle = {
                'width':  Math.max(43, maxStyle),
                'height': Math.max(43, maxStyle),
                'left':left_top.left,
                'top':left_top.top
            };
            if(isMove&&setStyle.width>30){
                _self.$box.css(setStyle);
            }
            if(typeof _self.defaults.coorMove == "function"){
                if(typeof _self.defaults.advert == "object"){
                    _self.defaults.coorMove(_self.$box.width(),left_top.left,left_top.top,_self.defaults.advert);
                }else{
                    _self.defaults.coorMove(_self.$box.width(),left_top.left,left_top.top);
                }
            }

            //var pageX = initXY(e, "x") - _self.posix.startPageX;
            //var pageY = initXY(e, "y") - _self.posix.startPageY;
            _self.posix = {
                'w': _self.$box.width(),
                'h': _self.$box.height(),
                'startPageX': initXY(e, "x"),
                'startPageY': initXY(e, "y")
            };
        }
    }
    function mouseup(e){
        event = EventUtil.getEvent(event); // 跨浏览器获取 event 对象
        EventUtil.preventDefault(event); // 阻止默认事件
        EventUtil.remove(document, eventType("moveEvent"), mousemove);
        EventUtil.remove(document, eventType("endEvent"), mouseup);
        if(typeof _self.defaults.upFuc == "function"){
            if(typeof _self.defaults.advert == "object"){
                _self.defaults.upFuc(_self.defaults.advert);
            }else{
                _self.defaults.upFuc();
            }
        }
    }
    function getChangePage(pageX,pageY,direction){
        var changePage = 0;
        if(direction == "x"){
            changePage = pageX;
            if(pageX<0&&pageY<0){
                if(pageX<pageY){
                    changePage = pageY;
                }
            }else if(pageX>0&&pageY>0){
                if(pageX>pageY){
                    changePage = pageY;
                }
            }else{
                changePage = 0;
            }
        }else if(direction == "y"){
            changePage = pageY;
            if(pageX>0&&pageY<0){
                if(-pageX<pageY){
                    changePage = -pageX;
                }
            }else if(pageX<0&&pageY>0){
                if(-pageX<pageY){
                    changePage = -pageX;
                }
            }else{
                changePage = 0;
            }
        }
        return changePage
    }
    function setMaxStyle(max_height,left_top,max_width){
        var maxStyle,isMove=true;
        if(max_height>_self.bigStyle.height-left_top.top){
            max_height = _self.bigStyle.height-left_top.top;
            isMove = false;
        }
        if(max_width>_self.bigStyle.width-left_top.left){
            max_width = _self.bigStyle.width-left_top.left;
            isMove = false;
        }
        maxStyle = max_width;
        if(max_width>max_height){
            maxStyle = max_height
        }
        return {
            maxStyle:maxStyle,
            isMove:isMove
        };
    }
}

// 事件处理对象
function eventUtil(){
    return {
        // 添加事件监听
        add: function(element, type, callback){
            if(element.addEventListener){
                element.addEventListener(type, callback, false);
            } else if(element.attachEvent){
                element.attachEvent('on' + type, callback);
            } else {
                element['on' + type] = callback;
            }
        },
        // 移除事件监听
        remove: function(element, type, callback){
            if(element.removeEventListener){
                element.removeEventListener(type, callback, false);
            } else if(element.detachEvent){
                element.detachEvent('on' + type, callback);
            } else {
                element['on' + type] = null;
            }

        },
        // 跨浏览器获取 event 对象
        getEvent: function(event){
            return event ? event : window.event;
        },

        // 跨浏览器获取 target 属性
        getTarget: function(event){
            return event.target || event.srcElement;
        },

        // 阻止事件的默认行为
        preventDefault: function(event){
            if(event.preventDefault){
                event.preventDefault();
            } else {
                event.returnValue = false;
            }
        },
        // 阻止事件流或使用 cancelBubble
        stopPropagation: function(){
            if(event.stopPropagation){
                event.stopPropagation();
            } else {
                event.cancelBubble = true;
            }
        }
    }
}
//返回绑定事件
function eventType(type){
    var hasTouch = 'ontouchstart' in window;
    var eventObj = {
        startEvent: hasTouch ? 'touchstart' : 'mousedown',
        moveEvent: hasTouch ? 'touchmove' : 'mousemove',
        endEvent: hasTouch ? 'touchend' : 'mouseup',
        cancelEvent: hasTouch ? 'touchcancel' : 'mouseup'
    };
    return eventObj[type];
}
// 判断响应时间事件决定初始值
function initXY(e, type) {
    var hasTouch = 'ontouchstart' in window;
    var valueObj = {
        x: hasTouch ? e.targetTouches[0].pageX : e.pageX,
        y: hasTouch ? e.targetTouches[0].pageY : e.pageY
    };
    return valueObj[type];
}

//数组排序
function compare(property,order){
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        if(order){
            return value2 - value1;
        }
        return value1 - value2;
    }
}